type EventName = string;
type Callback = Function;

class EventManager {

  events: Record<any, any>;

  constructor() {
    this.events = {};
  }

  on(eventName: EventName, callback: Callback) {
    if (!this.events[eventName]) {
      this.events[eventName] = [];
    }
    this.events[eventName].push(callback);
  }

  off(eventName: EventName, callback: Callback) {
    if (!this.events[eventName]) {
      return;
    }
    this.events[eventName] = this.events[eventName].filter((cb: Callback) => cb!== callback);
  }

  trigger(eventName: EventName, ...args: any[]) {
    if (!this.events[eventName]) {
      return;
    }
    this.events[eventName].forEach((callback: Callback) => callback(...args));
  }
}

export const eventManager = new EventManager();
